<?php

namespace App\Http\Controllers\Foundation;


use App\Model\Foundation\TeachLevel;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\Http\Controllers\MessagePacket;


class TeachLevelController extends BaseController
{
    use MessagePacket;


    /**
     * 获取单条信息
     * @param Request $request
     * @param $id
     * @return array
     */
    function get(Request $request, $id)
    {
        try{
            $obj = TeachLevel::find($id);
            return $this->success($obj);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }

    /**
     * 获取所有子级分类
     * @param Request $request
     * @param $id
     * @return array
     */
    function getChilds(Request $request, $id)
    {
        try{
            $query = TeachLevel::where('pid', $id);
            if($request->has('status')){
                $query->where('status', $request->status);
            }
            $objs = $query->get();
            return $this->success($objs);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }

    /**
     * 获取所有
     * @param Request $request
     * @return array
     */
    function getAll(Request $request)
    {
        try{
            $query = TeachLevel::whereNotNull('id');
            if($request->has('status')){
                $query->where('status', $request->status);
            }
            $objs = $query->get();
            return $this->success($objs);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }


    /**
     * 创建
     * @param Request $request
     * @return array
     */
    function create(Request $request)
    {
        try{
            $fileds = $request->all();
            $obj = TeachLevel::where('pid',$request->get('pid'))
                ->where('title',$request->get('title'))
                ->first();
            if($obj){
                $fileds['title'] = $request->get('title').'('.substr(md5(time()),0,6).')';
            }
            $obj = TeachLevel::firstOrCreate($fileds);
            $obj->save();
            return $this->success($obj);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }

    /**
     * 修改
     * @param Request $request
     * @return array
     */
    function update(Request $request, $id)
    {
        try{
            $obj = TeachLevel::find($id);
            $newobj = TeachLevel::where('pid',$request->get('pid'))
                ->where('title',$request->get('title'))
                ->where('id','!=',$id)
                ->first();
            if($newobj){
                return $this->fail($obj,'存在相同名称的级别');
            }
            $obj->update($request->all());
            return $this->success(true);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }


    /**
     * 禁用某个
     * @param Request $request
     * @param $id
     * @return array
     */
    function disable(Request $request, $id)
    {
        try{
            $this->disablebyid($id);
            return $this->success(true);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }

    protected function disablebyid($id)
    {
        $obj = TeachLevel::find($id);
        $obj->status = TeachLevel::DISABLE;
        $obj->save();
        $childs = TeachLevel::where('pid',$id)->get();
        if($childs){
            foreach ($childs as $child){
                $this->disablebyid($child->id);
            }
        }
    }


    /**
     * 启用某个
     * @param Request $request
     * @param $id
     * @return array
     */
    function enable(Request $request, $id)
    {
        try{
            $obj = TeachLevel::find($id);
            $obj->status = TeachLevel::ENABLE;
            $obj->save();
            return $this->success(true);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }


}
